﻿using System;
using System.Windows.Forms;

namespace FIS.iDET
{
    public class ClipboardUtils
    {
        
        public static void OnDataGridPaste(object grid, KeyEventArgs e)
        {
            //MessageBox.Show(e.KeyCode.ToString());
            if (e.Control ) 
                if ( e.KeyCode == Keys.V)
                    PasteFromClipboard((DataGridView)grid);
                else if ( e.KeyCode == Keys.Oem7)
                    PasteFromUpper((DataGridView)grid);
            if (e.KeyCode == Keys.Delete)
                DeleteSelectedArea((DataGridView)grid);
        }

        public static void DeleteSelectedArea(DataGridView grid)
        {
            for (int i = 0; i < grid.SelectedCells.Count; i++)
                grid.SelectedCells[i].Value = null;
        }
        public static void PasteFromUpper(DataGridView grid)
        {
            int currentRow = grid.SelectedCells[0].RowIndex;
            int currentColumn = grid.SelectedCells[0].ColumnIndex;
            if (grid.SelectedCells.Count > 1)
            {
                // choose the 1st cell
                for (int i = 1; i < grid.SelectedCells.Count; i++)
                {
                    if (currentRow > grid.SelectedCells[i].RowIndex)
                        currentRow = grid.SelectedCells[i].RowIndex;
                    if (currentColumn > grid.SelectedCells[i].ColumnIndex)
                        currentColumn = grid.SelectedCells[i].ColumnIndex;
                }
                DataGridViewCell cell = grid[currentColumn, currentRow];
                // if 1st cell null
                if (cell.Value == null && currentRow>0 )
                    if (!cell.ReadOnly) //H.NH add
                        cell.Value = grid[currentColumn, currentRow - 1].Value; //choose the upper cell
                for (int i = 0; i < grid.SelectedCells.Count; i++)
                {
                    if (!grid.SelectedCells[i].ReadOnly)
                        grid.SelectedCells[i].Value = cell.Value; //H.NH add
                }
            }
            else
            {
                if (currentRow > 0)
                    grid[currentColumn, currentRow].Value = grid[currentColumn, currentRow - 1].Value;
            }
        }

        public static void PasteFromClipboard(DataGridView grid)
        {
            try
            {
                char[] rowSplitter = { '\r', '\n' };
                char[] columnSplitter = { '\t' };

                // Get the text from Clipboard
                IDataObject dataInClipboard = Clipboard.GetDataObject();
                string stringInClipboard = (string)dataInClipboard.GetData(DataFormats.StringFormat);
                // split into rows
                string[] rowInClipboard = stringInClipboard.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries);
                // get current cell
                int currentRow = grid.SelectedCells[0].RowIndex;
                int currentColumn = grid.SelectedCells[0].ColumnIndex;
                // get 1st cell in selected area
                for (int i = 1; i < grid.SelectedCells.Count; i++)
                {
                    if (currentRow > grid.SelectedCells[i].RowIndex)
                        currentRow = grid.SelectedCells[i].RowIndex;
                    if (currentColumn > grid.SelectedCells[i].ColumnIndex)
                        currentColumn = grid.SelectedCells[i].ColumnIndex;
                }
                // add more rows if need to paste data
                /* if (grid.Rows.Count < rowInClipboard.Length + currentRow)
                    grid.Rows.Add(rowInClipboard.Length + currentRow - grid.Rows.Count); */

                // paste
                for (int iRow = 0; iRow < rowInClipboard.Length; iRow++)
                {
                    if (iRow + currentRow < grid.Rows.Count)
                    {
                        string[] cellsInRow = rowInClipboard[iRow].Split(columnSplitter);
                        for (int iCol = 0; iCol < cellsInRow.Length; iCol++)
                        {
                            if (grid.ColumnCount > currentColumn + iCol)
                            {
                                DataGridViewCell currentCell = grid.Rows[currentRow + iRow].Cells[currentColumn + iCol];
                                if (!currentCell.ReadOnly) // H.NH added to avoid Read only case.
                                    currentCell.Value = cellsInRow[iCol];
                            }
                        }
                    }
                    else break;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Sorry, can not paste from the clipboard.\nError: " + e.Message, "Paste Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}